package com.netease.web;

import com.netease.domain.MicroserviceDetail;
import com.netease.domain.MicroserviceItem;
import com.netease.domain.NamespaceItem;
import com.netease.service.MicroserviceService;
import com.netease.service.NamespaceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.LongAccumulator;

/**
 * Created by dongle on 2017/2/28.
 */
@org.springframework.web.bind.annotation.RestController
@RequestMapping(value="/api")
public class RestController {
    @Autowired
    private MicroserviceService microserviceService;
    @Autowired
    private NamespaceService namespaceService;

    @ApiOperation(value="获取微服务Id", notes="根据空间名和服务名来获取微服务id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spaceName", value = "空间名称", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "serviceName", value = "微服务名称", required = true, dataType = "String", paramType = "path"),
    })
    @RequestMapping(value="serviceidbyname/{spaceName}/{serviceName}", method= RequestMethod.GET)
    public Long getServiceIdByName(@PathVariable String spaceName, @PathVariable String serviceName) throws IOException {
        List<NamespaceItem> spaceList = namespaceService.getList();
        for (NamespaceItem item:
                spaceList) {
            if (spaceName.equals(item.getDisplay_name())){
                List<MicroserviceItem> serviceList = microserviceService.getListsInNamesapce(item.getId());
                for (MicroserviceItem sitem :
                        serviceList) {
                    if (serviceName.equals(sitem.getService_name())) {
                        return sitem.getId();
                    }
                }
            }
        }
        return null;
    }

    @ApiOperation(value="获取微服务详情", notes="根据url的id来获取微服务详细信息")
    @ApiImplicitParam(name = "id", value = "微服务ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value="service/{id}", method= RequestMethod.GET)
    public MicroserviceDetail getServiceDetail(@PathVariable Long id) throws IOException {
        return microserviceService.getDetail(id);
    }

    @ApiOperation(value="重启微服务", notes="根据url的id来重启微服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "微服务ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "option", value = "restart（重启）/ hard_reboot（冷重启）", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value="service/{id}/restart", method= RequestMethod.PUT)
    public String rebootService(@PathVariable Long id, @RequestParam String option) throws IOException {
        return microserviceService.restartService(id, option);
    }

    @ApiOperation(value="扩容微服务", notes="弹性伸缩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "微服务ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "new_replicas", value = "要扩容的副本数", required = true, dataType = "Integer", paramType = "query", allowableValues="range[1,5]")
    })
    @RequestMapping(value="service/{id}/elastic", method= RequestMethod.PUT)
    public String rebootService(@PathVariable Long id, @RequestParam Integer new_replicas) throws IOException {
        return microserviceService.elasticScaleService(id, new_replicas);
    }

    @ApiOperation(value="更改服务镜像版本", notes="更改服务容器到新的镜像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "微服务ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "container_id", value = "要更新的容器id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "image_path", value = "镜像地址：hub.c.163.com/public/ubuntu:14.04", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "update_interval", value = "逐个更新副本时，各个副本的间隔", required = true, dataType = "Integer", paramType = "query", allowableValues="range[1,600]")
    })
    @RequestMapping(value="service/{id}/updateimage", method= RequestMethod.PUT)
    public void updateServiceImage(@PathVariable Long id, @RequestParam Long container_id, @RequestParam String image_path, @RequestParam Integer update_interval) throws IOException {
        microserviceService.updateImage(id, container_id, image_path, update_interval);
    }

    // 根据服务名系列
    @ApiOperation(value="根据服务名重启微服务", notes="根据url的服务名来重启微服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spaceName", value = "空间名称", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "serviceName", value = "微服务名称", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "option", value = "restart（重启）/ hard_reboot（冷重启）", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value="servicebyname/{spaceName}/{serviceName}/restart", method= RequestMethod.PUT)
    public String rebootServiceByName(@PathVariable String spaceName, @PathVariable String serviceName, @RequestParam String option) throws IOException {
        Long serviceId = this.getServiceIdByName(spaceName, serviceName);
        return microserviceService.restartService(serviceId, option);
    }

}
